//
//  WBEmoticonToolBar.swift
//  weibo
//
//  Created by 胡古古 on 2017/6/14.
//  Copyright © 2017年 hugugu. All rights reserved.
//

import UIKit




enum WBEmoticonType:Int {
    // 最近
    case recent = 1000
    // 默认
    case normal = 1001
    // emoji
    case emoji = 1002
    // 浪小花
    case lxh = 1003
}

// 代理
protocol WBEmoticonToolBarDelegate:NSObjectProtocol {
    
    func emoticonToolBarButtonDidSelected(type:WBEmoticonType)
}

class WBEmoticonToolBar: UIStackView {
    
    //代理属性
    weak var delegate:WBEmoticonToolBarDelegate?

    //记录上次点击的按钮
    private var lastButton:UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        self.axis = .horizontal
        
        // 等比例填充
        self.distribution = .fillEqually
        
        addChildButtom(imageName: "compose_emotion_table_left", tilte: "最近", type: .recent)
        addChildButtom(imageName: "compose_emotion_table_left", tilte: "默认", type: .normal)
        addChildButtom(imageName: "compose_emotion_table_left", tilte: "Emoji", type: .emoji)
        addChildButtom(imageName: "compose_emotion_table_left", tilte: "浪小花", type: .lxh)
        
    
    }
    
    private func addChildButtom(imageName:String,tilte:String,type:WBEmoticonType) {
        
        let button = UIButton()
        
        button.tag = type.rawValue
        
        button.setBackgroundImage(UIImage(named:imageName + "_normal"), for: .normal)
        button.setBackgroundImage(UIImage(named:imageName + "_selected"), for: .selected)
        
        button.addTarget(self, action: #selector(cilckButton(btn:)), for: .touchUpInside)
        // 设置文字
        button.setTitle(tilte, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor.white, for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .selected)
        
        button.adjustsImageWhenHighlighted = false
        
        addArrangedSubview(button)
        
        if type == .normal {
            
            button.isSelected = true
            
            lastButton = button
        }
    }
    
    // MARK: --按钮的监听
    @objc private func cilckButton(btn:UIButton) {
        
        if btn == lastButton {
            
            return
        }
        
        lastButton?.isSelected = false
        
        btn.isSelected = true
        
        lastButton = btn
        
        let type = WBEmoticonType(rawValue: btn.tag)!
        
        // 调用代理方法
        delegate?.emoticonToolBarButtonDidSelected(type: type)
        
    }
    
    func selectedButtonWithSection(section:Int) {
        
        let button = self.viewWithTag(section + 1000) as! UIButton
        
        if button == lastButton {
            
            return
        }
        
        lastButton?.isSelected = false
        
        button.isSelected = true
        
        lastButton = button
    }
}
